<?php
class Course extends CI_Controller{

	//返回数量
	function count(){
		$this->load->model('server/news_model');
		$cont=[
			'table'=>'course'
		];
		$data = $this->news_model->get_conunt($cont);  //返回数据的数组
		echo json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	//返回查询结果
	function select(){
		$this->load->model('server/news_model');
		$page = $_GET["page"];
		$limit = $_GET["limit"];
		$like = $_GET["like"]; //老师名字

		if ($like == null || $like == "") {
			$like = null;
		} else {
			$like = ['name' => $like];
		}

		$cont=[
			'table'=>'course',
			'like' => $like,
			'limit' => [($page - 1) * $limit, $limit], // 获取第一页 显示10行内容
			'order_by' => ['power' => 'DESC']
		];

		$data = $this->news_model->select_form($cont);  //返回数据的数组
		echo json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	//写入操作
	function write(){
		$this->load->model('server/news_model');
		$id = $_POST["id"];
		$name = $_POST["name"];
		$content = $_POST["content"];
		$power = $_POST["power"];
		$title_url= $_POST["title_url"];

		//根据id删除内容
		$cont = [
			'table' => 'course',
			'where'=>[
				'id'=>$id
			]
		];
		$this->news_model->delete($cont);

		$cont = [
			'table' => 'course',
			'data' => [
				'name' => $name,
				'content' => $content,
				'power' => $power,
				'title_url'=>$title_url
			]
		];

		$isok = $this->news_model->insert_one($cont);
		$content = [
			'code' => 200,
			'msg' => "写入信息成功！"
		];
		echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	function delete(){
		$this->load->model('server/news_model');
		$id = $_GET["id"];
		//根据id删除内容
		$cont = [
			'table' => 'course',
			'where'=>[
				'id'=>$id
			]
		];
		$isok =$this->news_model->delete($cont);
		echo json_encode($isok, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}
}
